package com.cyjt.operation.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cyjt.operation.R;
import com.cyjt.operation.adapter.SimInfoListAdapter;
import com.cyjt.operation.base.AppContext;
import com.cyjt.operation.base.HandlerMessageCodes;
import com.cyjt.operation.bean.SimStatusInfo;
import com.cyjt.operation.fragment.DialogFragmentForDeviceStatusDetails;
import com.cyjt.operation.view.ActionBarLayout;

/**
 * 3G路由(SIM卡)列表的Activity
 * 
 * @author wsw
 * 
 */
public class RoutingListActivity extends Activity implements OnClickListener,
		OnItemClickListener {
	/** 切换正异常3G路由状态列表的Button */
	private Button button_switch_routing_status_list;
	/** 3G路由状态的listView */
	private ListView listView_routing_status_list;
	/** 提示信息（当列表为空时提示） */
	private TextView textView_sim_info;
	/** 所有路由列表的集合 */
	private List<SimStatusInfo> simInfoList;
	/** 异常路由列表的集合 */
	private List<SimStatusInfo> abnormalSimInfoList = new ArrayList<SimStatusInfo>();
	/** 正常路由列表的集合 */
	private List<SimStatusInfo> normalSimInfoList = new ArrayList<SimStatusInfo>();
	private SimInfoListAdapter simInfoListAdapter;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HandlerMessageCodes.FETCH_SIM_STATUS_LIST_SUCCEED:
				ActionBarLayout.isShowProgressBar(false);
				simInfoList = (List<SimStatusInfo>) msg.obj;
				separateRoutingListData();// 分离路由列表中正异常状态的数据
				loadAbnomalRoutingListData();// 加载非正常的路由列表数据
				break;
			case HandlerMessageCodes.FETCH_SIM_STATUS_LIST_FAILED:
				ActionBarLayout.isShowProgressBar(false);
				Toast.makeText(RoutingListActivity.this, "从服务器获取到的路由信息为空",
						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_routing_list);
		initView();
		// 从服务器获取3G路由列表的状态列表信息
		AppContext
				.getInstance()
				.getVolleyTools()
				.fetchSimStatusInfos(handler,
						AppContext.getInstance().getUser());

	}

	/**
	 * 初始化控件方法
	 */
	private void initView() {
		ActionBarLayout.setTitle("3G路由列表");
		ActionBarLayout.isShowProgressBar(true);
		textView_sim_info = (TextView) findViewById(R.id.textView_sim_info);
		listView_routing_status_list = (ListView) findViewById(R.id.listView_routing_status_list);
		listView_routing_status_list.setOnItemClickListener(this);
		button_switch_routing_status_list = (Button) findViewById(R.id.button_switch_routing_status_list);
		button_switch_routing_status_list.setOnClickListener(this);
	}

	/**
	 * 分开路由列表正异常状态的数据
	 */
	private void separateRoutingListData() {
		Collections.sort(simInfoList);
		for (SimStatusInfo simInfos : simInfoList) {
			Log.d("LTP", simInfos.getSimNo());
			if (simInfos.getStatus() == 2) {
				abnormalSimInfoList.add(simInfos);
			} else {
				normalSimInfoList.add(simInfos);
			}
		}
	}

	/**
	 * 加载非正常的路由列表数据
	 */
	private void loadAbnomalRoutingListData() {
		textView_sim_info.setText("");
		if (abnormalSimInfoList.isEmpty()) {
			textView_sim_info.setText("异常3G路由列表为空");
		}
		simInfoListAdapter = new SimInfoListAdapter(this, abnormalSimInfoList);
		listView_routing_status_list.setAdapter(simInfoListAdapter);
	}

	/**
	 * 加载正常的路由列表数据
	 */
	private void loadNormalRoutingListData() {
		textView_sim_info.setText("");
		if (normalSimInfoList.isEmpty()) {
			textView_sim_info.setText("正常3G路由列表为空");
		}
		simInfoListAdapter = new SimInfoListAdapter(this, normalSimInfoList);
		listView_routing_status_list.setAdapter(simInfoListAdapter);
	}

	@Override
	public void onClick(View v) {
		// TODO 控件的点击事件
		switch (v.getId()) {
		case R.id.button_switch_routing_status_list:
			if ("查看正常3G路由".equals(button_switch_routing_status_list.getText())) {
				loadNormalRoutingListData();
				button_switch_routing_status_list.setText("查看异常3G路由");
			} else {
				loadAbnomalRoutingListData();
				button_switch_routing_status_list.setText("查看正常3G路由");
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO ListView的点击事件
		SimStatusInfo simStatusInfo;
		if ("查看正常3G路由".equals(button_switch_routing_status_list.getText())) {
			simStatusInfo = abnormalSimInfoList.get(position);
		} else {
			simStatusInfo = normalSimInfoList.get(position);
		}
		Log.d("LTP", simStatusInfo.getSimNo() + "");
		String routingStatusInfoDetails = "SIM卡号：" + simStatusInfo.getSimNo()
				+ "\n\n设备编号：" + simStatusInfo.getDeviceCode() + "\n\n到期时间："
				+ simStatusInfo.getEndAtToString() + "\n\n充值时间："
				+ simStatusInfo.getUpAtToString() + "\n\n充值金额："
				+ simStatusInfo.getUpMoney() + "元";
		DialogFragmentForDeviceStatusDetails mDeviceStatusDetails = new DialogFragmentForDeviceStatusDetails();
		Bundle args = new Bundle();
		args.putString("text", routingStatusInfoDetails);
		// args.putString("title", "Sim卡详情");
		mDeviceStatusDetails.setArguments(args);
		mDeviceStatusDetails.setStyle(DialogFragment.STYLE_NO_TITLE,
				android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth);
		mDeviceStatusDetails.show(getFragmentManager(), "RoutingStatusDetail");
	}
}
